package com.lanou.renrenxin.fragment;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMChatOptions;
import com.lanou.renrenxin.R;
import com.lanou.renrenxin.activity.BlackListActivity;
import com.lanou.renrenxin.activity.LoginActivity;
import com.lanou.renrenxin.activity.MainActivity;

import com.lanou.renrenxin.custommodule.CircleImageView;

import com.lanou.renrenxin.activity.PersonalActivity;
import com.lanou.renrenxin.scanmodel.CreateQRCode;

import com.lanou.renrenxin.tools.Constant;
import com.lanou.renrenxin.tools.EaseSwitchButton;
import com.lanou.renrenxin.tools.RenRenXinHelper;
import com.lanou.renrenxin.tools.RenRenXinModel;

/**
 * Created by zy on 16/3/1.
 */
public class SetFragment extends Fragment implements View.OnClickListener {
    //退出按钮
    private Button btn_login_out;
    /**
     * 设置新消息通知布局
     */
    private LinearLayout ll_switch_notification;
    /**
     * 设置声音布局
     */
    private LinearLayout ll_switch_sound;
    /**
     * 设置震动布局
     */
    private LinearLayout ll_switch_shake;
    /**
     * 设置扬声器布局
     */
    private LinearLayout ll_switch_speaker;
    //黑名单布局
    private LinearLayout ll_blacklistContainer;
    //个人信息布局
    private RelativeLayout rl_personal;

    private CircleImageView headImage;

    private EaseSwitchButton switch_notification;
    private EaseSwitchButton switch_sound;
    private EaseSwitchButton switch_shake;
    private EaseSwitchButton switch_speaker;
    private ImageButton imageButton_qrCode;
    private RenRenXinModel settingsModel;

    private TextView tvUsername;
    private Context context;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        context = activity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_set, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        btn_login_out = (Button) getView().findViewById(R.id.login_out);

        ll_switch_notification = (LinearLayout) getView().findViewById(R.id.ll_switch_notification);
        ll_switch_sound = (LinearLayout) getView().findViewById(R.id.ll_switch_sound);
        ll_switch_shake = (LinearLayout) getView().findViewById(R.id.ll_switch_shake);
        ll_switch_speaker = (LinearLayout) getView().findViewById(R.id.ll_switch_speaker);
        ll_blacklistContainer = (LinearLayout) getView().findViewById(R.id.ll_black_list);
        headImage = (CircleImageView) getView().findViewById(R.id.personal_touXiang);
        rl_personal = (RelativeLayout) getView().findViewById(R.id.rl_personal);

        switch_notification = (EaseSwitchButton) getView().findViewById(R.id.switch_notification);
        switch_sound = (EaseSwitchButton) getView().findViewById(R.id.switch_sound);
        switch_shake = (EaseSwitchButton) getView().findViewById(R.id.switch_shake);
        switch_speaker = (EaseSwitchButton) getView().findViewById(R.id.switch_speaker);
        imageButton_qrCode = (ImageButton) getView().findViewById(R.id.imBtn_qrCode);

        btn_login_out.setOnClickListener(this);
        imageButton_qrCode.setOnClickListener(this);
        ll_switch_notification.setOnClickListener(this);
        ll_switch_sound.setOnClickListener(this);
        ll_switch_shake.setOnClickListener(this);
        ll_switch_speaker.setOnClickListener(this);
        ll_blacklistContainer.setOnClickListener(this);
        rl_personal.setOnClickListener(this);


        //个人信息昵称
        tvUsername = (TextView) getView().findViewById(R.id.personal_niCheng);
        tvUsername.setText(EMChatManager.getInstance().getCurrentUser());

        settingsModel = new RenRenXinModel(context);
        // 震动和声音总开关，来消息时，是否允许此开关打开
        if (settingsModel.getSettingMsgNotification()) {
            switch_notification.openSwitch();
        } else {
            switch_notification.closeSwitch();
        }
        // 是否打开声音
        if (settingsModel.getSettingMsgSound()) {
            switch_sound.openSwitch();
        } else {
            switch_sound.closeSwitch();
        }
        // 是否打开震动
        if (settingsModel.getSettingMsgVibrate()) {
            switch_shake.openSwitch();
        } else {
            switch_shake.closeSwitch();
        }
        // 是否打开扬声器
        if (settingsModel.getSettingMsgSpeaker()) {
            switch_speaker.openSwitch();
        } else {
            switch_speaker.closeSwitch();
        }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_out:  //退出登录
                logout();
                break;
            case R.id.ll_switch_notification:
                if (switch_notification.isSwitchOpen()) {
                    switch_notification.closeSwitch();
                    ll_switch_sound.setVisibility(View.GONE);
                    ll_switch_shake.setVisibility(View.GONE);

                    settingsModel.setSettingMsgNotification(false);
                    // 获取到EMChatOptions对象
                    EMChatOptions chatOptions = EMChatManager.getInstance().getChatOptions();
                    chatOptions.setShowNotificationInBackgroud(false);

                } else {
                    switch_notification.openSwitch();
                    ll_switch_sound.setVisibility(View.VISIBLE);
                    ll_switch_shake.setVisibility(View.VISIBLE);

                    settingsModel.setSettingMsgNotification(true);
                    EMChatOptions chatOptions = EMChatManager.getInstance().getChatOptions();
                    chatOptions.setShowNotificationInBackgroud(true);
                }
                break;
            case R.id.ll_switch_sound:
                if (switch_sound.isSwitchOpen()) {
                    switch_sound.closeSwitch();
                    settingsModel.setSettingMsgSound(false);
                    EMChatOptions chatOptions = EMChatManager.getInstance().getChatOptions();
                    chatOptions.setNoticeBySound(false);
                } else {
                    switch_sound.openSwitch();
                    settingsModel.setSettingMsgSound(true);
                    EMChatOptions chatOptions = EMChatManager.getInstance().getChatOptions();
                    chatOptions.setNoticeBySound(true);
                }
                break;
            case R.id.ll_switch_shake:
                if (switch_shake.isSwitchOpen()) {
                    switch_shake.closeSwitch();
                    settingsModel.setSettingMsgVibrate(false);
                    EMChatOptions chatOptions = EMChatManager.getInstance().getChatOptions();
                    chatOptions.setNoticedByVibrate(false);
                } else {
                    switch_shake.openSwitch();
                    settingsModel.setSettingMsgVibrate(true);
                    EMChatOptions chatOptions = EMChatManager.getInstance().getChatOptions();
                    chatOptions.setNoticedByVibrate(true);
                }
                break;
            case R.id.ll_switch_speaker:
                if (switch_speaker.isSwitchOpen()) {
                    switch_speaker.closeSwitch();
                    settingsModel.setSettingMsgSpeaker(false);
                    EMChatOptions chatOptions = EMChatManager.getInstance().getChatOptions();
                    chatOptions.setUseSpeaker(false);
                } else {
                    switch_speaker.openSwitch();
                    settingsModel.setSettingMsgSpeaker(true);
                    EMChatOptions chatOptions = EMChatManager.getInstance().getChatOptions();
                    chatOptions.setUseSpeaker(true);
                }
                break;
            case R.id.ll_black_list:
                startActivity(new Intent(getActivity(), BlackListActivity.class));
                break;
            case R.id.rl_personal:
                startActivity(new Intent(getActivity(), PersonalActivity.class));
                break;
            case R.id.imBtn_qrCode:
                showPersonalQrCodeDialog();
        }
    }
    //二维码dialog
    private void showPersonalQrCodeDialog() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View view = LayoutInflater.from(this.getActivity()).inflate(R.layout.dialog_qr_code, null);
        builder.setView(view);
        final TextView textView = (TextView) view.findViewById(R.id.dialog_input_content);
        final ImageView imageViewShow = (ImageView) view.findViewById(R.id.dialog_img_show);
        textView.setText(EMChatManager.getInstance().getCurrentUser());
        //调用生成二维码方法
        String context = tvUsername.getText().toString();
        createQRImage(context,imageViewShow);

        builder.show();
    }
    //生成二维码方法
    private void createQRImage(String content, final ImageView imageViewShow) {
        CreateQRCode.createQRImage(content, "test", new CreateQRCode.OnCreateQRListener() {
            @Override
            public void onSuccess(Bitmap qrImage) {
                imageViewShow.setImageBitmap(qrImage);

            }
        });
    }

    //退出登录
    void logout() {
        final ProgressDialog pd = new ProgressDialog(getActivity());
        String st = getResources().getString(R.string.Are_logged_out);
        pd.setMessage(st);
        pd.setCanceledOnTouchOutside(false);
        pd.show();
        RenRenXinHelper.getInstance().logout(false, new EMCallBack() {
            @Override
            public void onSuccess() {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        pd.dismiss();
                        //重新显示登录页面
                        ((MainActivity) getActivity()).finish();
                        startActivity(new Intent(getActivity(), LoginActivity.class));
                    }
                });
            }

            @Override
            public void onError(int i, String s) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        pd.dismiss();
                        Toast.makeText(getActivity(), "退出登录失败！", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onProgress(int i, String s) {

            }
        });
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (((MainActivity) getActivity()).isConflict) {
            outState.putBoolean("isConflict", true);
        } else if (((MainActivity) getActivity()).getCurrentAccountRemoved()) {
            outState.putBoolean(Constant.ACCOUNT_REMOVED, true);
        }
    }
}
